package com.ssmTest.service.impl;

import com.ssmTest.service.FileService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.UUID;

@Service("fileService")
public class FileServiceImpl implements FileService {
  @Override
  public String uploadFile(MultipartFile file, String load) {
    //==================================文件处理start==================================
    String dif = UUID.randomUUID().toString().substring(0, 4); //通过uuid生成不同的字符来区分
    //注意 一下默认windows存储
    String path = com.ssmTest.utils.ToolUtil.getPropert("filepath.properties", "imgPath");

    String filename = Calendar.getInstance().getTimeInMillis() + dif + ".jpg";
    String fileStore = path + filename;//真实存储路径
    //以下是linux存储

    //   String fileStore = request.getSession().getServletContext().getRealPath("/")+"files//"+username+"//"+dif+"//";//存储路径
    File dest = new File(fileStore);
    // 检测是否存在目录
    if (!dest.getParentFile().exists()) {
      dest.getParentFile().mkdirs();//生成文件
    }
    try {
      file.transferTo(dest);
    } catch (IllegalStateException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return filename;
  }
}
